package com.have.blog.base.wrapper;

import lombok.Data;

import java.io.Serializable;

@Data
public class Wrapper<T> implements Serializable {
    /**
     * 序列化标识
     */
    private static final long serialVersionUID = 4893280118017319089L;

    private Integer code;
    private String message;
    private T data;


    protected Wrapper() {}

    protected Wrapper(int code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Wrapper<T> success(T data) {
        return new Wrapper<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Wrapper<T> success(T data, String message) {
        return new Wrapper<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static <T> Wrapper<T> success() {
        return new Wrapper<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }

    public static <T> Wrapper<T> successWithMessage(String message) {
        return new Wrapper<>(ResultCode.SUCCESS.getCode(), message, null);
    }

    public static <T> Wrapper<T> failed() {
        return new Wrapper<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
    }

    public static <T> Wrapper<T> failed(String message) {
        return new Wrapper<>(ResultCode.FAILED.getCode(), message, null);
    }

    public static <T> Wrapper<T> failed(int code, String message) {
        return new Wrapper<>(code, message, null);
    }

    public static <T> Wrapper<T> illegal() {
        return new Wrapper<>(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMessage(), null);
    }

    public static <T> Wrapper<T> illegal(String msg) {
        return new Wrapper<>(ResultCode.VALIDATE_FAILED.getCode(), msg, null);
    }

    public static <T> Wrapper<T> unauthorized() {
        return new Wrapper<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), null);
    }

    public static <T> Wrapper<T> forbidden() {
        return new Wrapper<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), null);
    }

    public static <T> Wrapper<T> codeAndMessage(int code, String message) {
        return new Wrapper<>(code, message, null);
    }

    public boolean isSuccess() {
         return ResultCode.SUCCESS.getCode() == this.code;
    }

    public boolean isError() {
        return !isSuccess();
    }

    public int getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
